package com.authsystem.service.impl;

import com.authsystem.entity.User;
import com.authsystem.mapper.UserMapper;
import com.authsystem.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Override
    public User login(String loginname, String password) {
        return userMapper.selectByLoginnameAndPassword(loginname, password);
    }

    @Override
    public User getUserWithRolesByUsername(String username) {
        return userMapper.selectUserWithRolesByUsername(username);
    }

    @Override
    public List<User> getUsersByCondition(String username, String email, Integer status,
                                          String name, String mobilephone, Integer sex,
                                          Integer page, Integer pageSize) {
        // 计算偏移量
        int offset = (page - 1) * pageSize;
        return userMapper.selectUsersByCondition(username, email, status, name, mobilephone, sex, offset, pageSize);
    }

    @Override
    public Long countUsersByCondition(String username, String email, Integer status,
                                      String name, String mobilephone, Integer sex) {
        return userMapper.countUsersByCondition(username, email, status, name, mobilephone, sex);
    }

    @Override
    public Long getUserCount() {
        return userMapper.countUsers();
    }
}